package com.service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

//@Service
public class RedisService
{
    @Resource
    private RedisTemplate redisTemplate;

    //验证码存入redis的key标识
    private String codeName = "verifyCode";
    //验证码过期时间(默认值30秒)
    private Long workTime = 5*6000L;//5分钟

    //向redis服务其中存入验证码并设定过期时间
    public void saveCodeAndSetTime( String code )
    {
        redisTemplate.opsForValue().set( codeName , code , workTime , TimeUnit.SECONDS );
    }

    //从redis中取出指定的key
    public String getCode()
    {
        String code = (String)redisTemplate.opsForValue().get( codeName );

        return code;
    }

    //修改默认过期时间
    public void changeWorkTime( Long newWorkTime )
    {
        this.workTime = newWorkTime;
    }

}
